package com.example.activitidemo.service.event;

import com.example.activitidemo.dao.AssigneeRepository;
import com.example.activitidemo.dao.EventRepository;
import com.example.activitidemo.dto.Event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 张志鹏
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EventService {
    @Autowired
    private EventRepository eventRepository;
    @Autowired
    private AssigneeRepository assigneeRepository;

    /**
     * 新增事件
     *
     * @param event 实体
     */
    public void save(Event event) {
        event.date(new Date());
        eventRepository.save(event);
    }

    /**
     * 查询事件列表
     *
     * @return List<Event>
     */
    public List<Event> findAll() {
        List<Event> all = eventRepository.findByOrderByDateDesc();
        for (Event event : all) {
            event.assigneeName(assigneeRepository.getOne(event.getAssignee()).getName());
        }
        return all;
    }

    /**
     * 查询待办事件列表
     *
     * @return List<Event>
     */
    public List<Event> findUserTasksAll(String assignee) {
        List<Event> all = eventRepository.findAll(new Specification<Event>() {
            @Override
            public Predicate toPredicate(Root<Event> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();//查询条件集
                list.add(criteriaBuilder.equal(root.get("doingEmp").as(String.class), assignee));
                list.add(criteriaBuilder.isNull(root.get("finishTime").as(String.class)));
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        }, new Sort(Sort.Direction.DESC, "date"));
        for (Event event : all) {
            event.assigneeName(assigneeRepository.getOne(event.getDoingEmp()).getName());
        }
        return all;
    }

    public Event getOne(String id){
        return eventRepository.getOne(id);
    }

    /**
     * 保存流程实例ID
     *
     * @param id                事件ID
     * @param processInstanceId 流程实例ID
     */
    private void saveAndFlush(String id, String processInstanceId) {
        Event one = eventRepository.getOne(id);
        one.setProcessInstanceId(processInstanceId);
    }

    private void saveAndFlush(String id, Date finishTime) {
        Event one = eventRepository.getOne(id);
        one.setFinishTime(finishTime);
    }

    private void saveAndFlush(String id, String processInstanceId, String taskId, String doingEmp) {
        Event one = eventRepository.getOne(id);
        one.setProcessInstanceId(processInstanceId);
        one.setTaskId(taskId);
        one.setDoingEmp(doingEmp);
    }

    private void saveAndFlush(String id, String taskId, String doingEmp) {
        Event one = eventRepository.getOne(id);
        one.setTaskId(taskId);
        one.setDoingEmp(doingEmp);
    }
}
